package cn.libinal.scloud.base.controller;

import cn.libinal.scloud.base.common.dto.ClientDto;
import cn.libinal.scloud.base.common.dto.DtoMapper;
import cn.libinal.scloud.base.common.web.ResultJson;
import cn.libinal.scloud.base.entity.Client;
import cn.libinal.scloud.base.service.ClientService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author libin
 */
@RestController
@RequestMapping("client")
public class ClientController {
    private final ClientService clientService;
    private final DtoMapper dtoMapper;

    public ClientController(ClientService clientService, DtoMapper dtoMapper) {
        this.clientService = clientService;
        this.dtoMapper = dtoMapper;
    }

    @PostMapping("/register")
    public ResultJson clientRegistered(@RequestBody @Valid Client client) {
        return ResultJson.success(clientService.create(client));
    }

    @GetMapping(name = "根据客户端ID查询客户端信息", path = "findClientByClientId")
    public ResultJson<ClientDto> findClientByClientId(@RequestParam String clientId) {
        Client client = clientService.findClientByClientId(clientId);
        return ResultJson.success(dtoMapper.map(client, ClientDto.class));
    }
}
